//
//  FFPageTabView.swift
//  ZgwApp
//
//  Created by li on 2025-02-12.
//

import SwiftUI

struct FFPageTabView<D: RandomAccessCollection, C: View>: View where D.Element: Hashable {
	
	init(selected: D.Element?, data: D, @ViewBuilder content: @escaping (D.Element) -> C) {
		self.data = data
		self.selected = selected
		self.content = content
	}
	
	let data: D
	
	let selected: D.Element?
	@ViewBuilder
	var content: (D.Element) -> C
	
	@State private var scrollWidth: CGFloat = 0
//	@Namespace private var markNamespace
	
	@Environment(\.fFPageTabViewHorizontalAlginment) var fFPageTabViewHorizontalAlginment
	@Environment(\.fFPageTabViewHorizontalSpacing) var fFPageTabViewHorizontalSpacing
	
	var body: some View {
		
		
		ScrollView(.horizontal) {
			
			HStack(spacing: fFPageTabViewHorizontalSpacing) {
				ForEach(data, id: \.self) { element in
					ZStack {
						content(element)
						VStack(spacing: 0) {
							
							Spacer()
							
							markView()
								.ffHidden(value: selected != element)
						}
					}
				}
			}
			.frame(minWidth: scrollWidth, alignment: Alignment(horizontal: fFPageTabViewHorizontalAlginment, vertical: .center))
			
		}

	}
	
	func markView() -> some View {
		Color.red
			.frame(height: 2)
//			.matchedGeometryEffect(id: "Mark", in: markNamespace,  isSource: true)
	}
}

struct FFPageTabTestView: View {
	@State var list: [Int] = [1,2,3,4,5,6]
	@State var selected: Int = -1
	
	var body: some View {
		
		//		FFPageTabView(selected: $selected, data: list) { value in
		//			Text("\(value)")
		//		}
		Color.red
	}
}

#Preview {
	FFPageTabTestView()
}
